package com.app.newiptv.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.app.newiptv.R;
import com.app.newiptv.adaper.TestRecycleAdaper;
import com.app.newiptv.presenter.TestRecyclePresenter;
import com.app.newiptv.view.FocusGridLayoutManager;

public class TestRecycleActivity extends AppCompatActivity implements TestRecyclePresenter.Callback{
    private RecyclerView recyclerView;
    private TestRecyclePresenter presenter;
    private int LOAD_COMPLEAT = 1;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == LOAD_COMPLEAT){
                TestRecyclePresenter presenter = (TestRecyclePresenter) msg.obj;
                int count = msg.arg1;
                onBindData(presenter,count);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_recycle);
        initView();
        initData();
    }

    private void initData() {
        Bundle bundle = new Bundle();
        presenter = new TestRecyclePresenter();
        presenter.setDisplayed(true);
        presenter.loadItem(bundle,this);
    }

    private void initView() {
        recyclerView = (RecyclerView) this.findViewById(R.id.test_recycle);
    }
    public void onBindData(TestRecyclePresenter presenter,int count){
        TestRecycleAdaper adapter = new TestRecycleAdaper(presenter);
        recyclerView.setLayoutManager(new FocusGridLayoutManager(this, 5) );
        adapter.setCount(count);
        recyclerView.setAdapter(adapter);
    }
    @Override
    public void onError(int code, String msg) {

    }

    @Override
    public void onMovieCompleted(TestRecyclePresenter presenter, int count) {
        Message msg = Message.obtain();
        msg.arg1 = count;
        msg.what = LOAD_COMPLEAT;
        msg.obj = presenter;
        handler.sendMessage(msg);
    }
}
